using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ThaiLanguageTools.UI
{
    public partial class RulesDebugPanel : UserControl
    {
        RomanizedLookup _lookup;

        public RulesDebugPanel()
        {
            InitializeComponent();
        }

        public RomanizedLookup Lookup
        {
            get { return _lookup; }
            set 
            { 
                _lookup = value;
                OnRulesChanged();
                lbRules.BackColor = SystemColors.Window;
            }
        }

        public event EventHandler RulesChanged;
        void OnRulesChanged()
        {
            lbRules.Items.Clear();
            if (_lookup != null)
            {
                foreach (ChangeRule r in _lookup.ChangeRules.Rules)
                {
                    lbRules.Items.Add(r);
                }
                lbRules.BackColor = Color.LightGoldenrodYellow;
            }
            if (RulesChanged != null) { RulesChanged(this, EventArgs.Empty); }
        }

        public void ShowQueries(QueryList queries)
        {
            lbQueries.Items.Clear();
            List<QueryInfo> queryStrings = queries.GetQueryListCopy(true);
            foreach (QueryInfo q in queryStrings)
            {
                lbQueries.Items.Add(q.Text + " " + q.Probability);
            }
        }

        private void bAddRule_Click(object sender, EventArgs e)
        {
            try
            {
                _lookup.ChangeRules.Rules.Add(ChangeRule.Parse(tbRule.Text));
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message);
            }
            tbRule.Text = String.Empty;
            OnRulesChanged();
        }

        private void bDeleteRule_Click(object sender, EventArgs e)
        {
            DeleteRule(lbRules.SelectedItem as ChangeRule);
            OnRulesChanged();
        }
        void DeleteRule(ChangeRule r)
        {
            if (r == null) { return; }
            _lookup.ChangeRules.Rules.Remove(r);
        }

        private void bReloadRules_Click(object sender, EventArgs e)
        {
            _lookup.ChangeRules = ChangeRuleList.Load(RomanizedLookup.RulesFile);
            OnRulesChanged();
            lbRules.BackColor = SystemColors.Window;
        }

        private void bSaveRules_Click(object sender, EventArgs e)
        {
            _lookup.ChangeRules.Save(RomanizedLookup.RulesFile);

            // Save back to the project (if in debug mode
            String projectRulesFile = Path.Combine(@"..\..\..\ThaiLanguageTools", RomanizedLookup.RulesFile);
            if (File.Exists(projectRulesFile))
            {
                _lookup.ChangeRules.Save(projectRulesFile);
            }

            lbRules.BackColor = SystemColors.Window;
        }

        private void tbRule_TextChanged(object sender, EventArgs e)
        {
            bDeleteRule.Enabled = !String.IsNullOrEmpty(tbRule.Text);
            bAddRule.Enabled = !String.IsNullOrEmpty(tbRule.Text);
        }

    }
}
